{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/llm/maritalk.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\n",
    "# Maritalk\n",
    "\n",
    "## Introduction\n",
    "\n",
    "MariTalk is an assistant developed by the Brazilian company [Maritaca AI](https://www.maritaca.ai).\n",
    "MariTalk is based on language models that have been specially trained to understand Portuguese well.\n",
    "\n",
    "This notebook demonstrates how to use MariTalk with Llama Index through two examples:\n",
    "\n",
    "1. Get pet name suggestions with chat method;\n",
    "2. Classify film reviews as negative or positive with few-shot examples with complete method."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation\n",
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index\n",
    "!pip install llama-index-llms-maritalk\n",
    "!pip install asyncio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## API Key\n",
    "You will need an API key that can be obtained from chat.maritaca.ai (\"Chaves da API\" section)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 1 - Pet Name Suggestions with Chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "from llama_index.llms.maritalk import Maritalk\n",
    "\n",
    "import asyncio\n",
    "\n",
    "# To customize your API key, do this\n",
    "# otherwise it will lookup MARITALK_API_KEY from your env variable\n",
    "llm = Maritalk(api_key=\"<your_maritalk_api_key>\", model=\"sabia-2-medium\")\n",
    "\n",
    "# Call chat with a list of messages\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\",\n",
    "        content=\"You are an assistant specialized in suggesting pet names. Given the animal, you must suggest 4 names.\",\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"I have a dog.\"),\n",
    "]\n",
    "\n",
    "# Sync chat\n",
    "response = llm.chat(messages)\n",
    "print(response)\n",
    "\n",
    "\n",
    "# Async chat\n",
    "async def get_dog_name(llm, messages):\n",
    "    response = await llm.achat(messages)\n",
    "    print(response)\n",
    "\n",
    "\n",
    "asyncio.run(get_dog_name(llm, messages))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Stream Generation\n",
    "\n",
    "For tasks involving the generation of long text, such as creating an extensive article or translating a large document, it can be advantageous to receive the response in parts, as the text is generated, instead of waiting for the complete text. This makes the application more responsive and efficient, especially when the generated text is extensive. We offer two approaches to meet this need: one synchronous and another asynchronous."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sync streaming chat\n",
    "response = llm.stream_chat(messages)\n",
    "for chunk in response:\n",
    "    print(chunk.delta, end=\"\", flush=True)\n",
    "\n",
    "\n",
    "# Async streaming chat\n",
    "async def get_dog_name_streaming(llm, messages):\n",
    "    async for chunk in await llm.astream_chat(messages):\n",
    "        print(chunk.delta, end=\"\", flush=True)\n",
    "\n",
    "\n",
    "asyncio.run(get_dog_name_streaming(llm, messages))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example 2 - Few-shot Examples with Complete\n",
    "\n",
    "We recommend using the `llm.complete()` method when using the model with few-shot examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"\"\"Classifique a resenha de filme como \"positiva\" ou \"negativa\".\n",
    "\n",
    "Resenha: Gostei muito do filme, é o melhor do ano!\n",
    "Classe: positiva\n",
    "\n",
    "Resenha: O filme deixa muito a desejar.\n",
    "Classe: negativa\n",
    "\n",
    "Resenha: Apesar de longo, valeu o ingresso..\n",
    "Classe:\"\"\"\n",
    "\n",
    "# Sync complete\n",
    "response = llm.complete(prompt)\n",
    "print(response)\n",
    "\n",
    "\n",
    "# Async complete\n",
    "async def classify_review(llm, prompt):\n",
    "    response = await llm.acomplete(prompt)\n",
    "    print(response)\n",
    "\n",
    "\n",
    "asyncio.run(classify_review(llm, prompt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sync streaming complete\n",
    "response = llm.stream_complete(prompt)\n",
    "for chunk in response:\n",
    "    print(chunk.delta, end=\"\", flush=True)\n",
    "\n",
    "\n",
    "# Async streaming complete\n",
    "async def classify_review_streaming(llm, prompt):\n",
    "    async for chunk in await llm.astream_complete(prompt):\n",
    "        print(chunk.delta, end=\"\", flush=True)\n",
    "\n",
    "\n",
    "asyncio.run(classify_review_streaming(llm, prompt))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
